<template>
  <div class="dashboard-container">
    <!-- 欢迎卡片 -->
    <div class="welcome-card">
      <div class="welcome-content">
        <h1 class="welcome-title">
          <el-icon class="welcome-icon"><User /></el-icon>
          欢迎您，{{ username }}
        </h1>
        <p class="welcome-subtitle">{{ currentDate }} {{ currentTime }}</p>
      </div>
    </div>

    <!-- 统计卡片区域 -->
    <div class="statistics-section">
      <div class="stat-card">
        <div class="stat-icon primary">
          <el-icon><DataAnalysis /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-label">系统累计登录次数</div>
          <div class="stat-value">{{ totalStats.loginCount }}</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon success">
          <el-icon><Mouse /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-label">系统累计点击次数</div>
          <div class="stat-value">{{ totalStats.clickCount }}</div>
        </div>
      </div>
    </div>

    <!-- 快捷入口区域 -->
    <div class="quick-access-section">
      <h3 class="section-title">快捷入口</h3>
      <div class="quick-access-grid">
        <div class="quick-access-card" @click="goToMyTasks">
          <div class="quick-icon primary">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="quick-content">
            <div class="quick-title">我的待办</div>
            <div class="quick-desc">查看待处理的任务</div>
          </div>
          <div class="quick-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="quick-access-card" @click="goToFormData">
          <div class="quick-icon success">
            <el-icon><Document /></el-icon>
          </div>
          <div class="quick-content">
            <div class="quick-title">表单数据</div>
            <div class="quick-desc">管理表单业务数据</div>
          </div>
          <div class="quick-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="quick-access-card" @click="goToMySeals">
          <div class="quick-icon warning">
            <el-icon><Stamp /></el-icon>
          </div>
          <div class="quick-content">
            <div class="quick-title">我的印章</div>
            <div class="quick-desc">管理个人印章信息</div>
          </div>
          <div class="quick-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>

        <div class="quick-access-card" @click="goToMyCerts">
          <div class="quick-icon info">
            <el-icon><Key /></el-icon>
          </div>
          <div class="quick-content">
            <div class="quick-title">我的证书</div>
            <div class="quick-desc">管理个人证书信息</div>
          </div>
          <div class="quick-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { User, Mouse, DataAnalysis, Clock, ArrowRight, Document, Stamp, Key } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { statisticsApi } from '@/api'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const router = useRouter()

// 用户信息
const username = computed(() => userStore.displayName || '用户')

// 跳转到我的待办
const goToMyTasks = () => {
  router.push('/home/taskcenter/mytask')
}

// 跳转到表单数据
const goToFormData = () => {
  router.push('/home/form/data')
}

// 跳转到我的印章
const goToMySeals = () => {
  router.push('/home/system/seal')
}

// 跳转到我的证书
const goToMyCerts = () => {
  router.push('/home/system/cert')
}

// 当前日期时间
const currentDate = ref('')
const currentTime = ref('')

// 统计数据
const loading = ref(false)
const totalStats = ref({
  loginCount: 0,
  clickCount: 0
})

// 更新日期时间
const updateDateTime = () => {
  const now = new Date()
  const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  
  currentDate.value = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${weekdays[now.getDay()]}`
  currentTime.value = now.toLocaleTimeString('zh-CN', { hour12: false })
}

// 加载统计数据
const loadStatistics = async () => {
  loading.value = true
  try {
    const totalRes = await statisticsApi.getTotalStatistics()

    if (totalRes.code === 200) {
      totalStats.value = totalRes.data || { loginCount: 0, clickCount: 0 }
    }
  } catch (error) {
    console.error('加载统计数据失败:', error)
    ElMessage.error('加载统计数据失败')
  } finally {
    loading.value = false
  }
}

// 页面挂载时初始化
onMounted(() => {
  updateDateTime()
  // 每秒更新时间
  setInterval(updateDateTime, 1000)
  
  // 加载统计数据
  loadStatistics()
  
  // 每30秒自动刷新统计数据
  setInterval(loadStatistics, 30000)
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 80px);
}

// 欢迎卡片
.welcome-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  padding: 32px;
  margin-bottom: 24px;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
}

.welcome-content {
  color: white;
}

.welcome-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 12px 0;
  
  .welcome-icon {
    font-size: 32px;
  }
}

.welcome-subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

// 统计卡片区域
.statistics-section {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  margin-bottom: 24px;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  }
}

.stat-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  
  &.primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
  }
  
  &.success {
    background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
    color: white;
  }
  
  &.warning {
    background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
    color: white;
  }
  
  &.danger {
    background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
    color: white;
  }
}

.stat-content {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
}

// 快捷入口区域
.quick-access-section {
  max-width: 1200px;
  margin: 0 auto;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 16px 0;
  background: transparent;
}

.quick-access-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.quick-access-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  cursor: pointer;
  transition: all 0.3s;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
    
    .quick-arrow {
      transform: translateX(4px);
    }
  }
}

.quick-icon {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  flex-shrink: 0;
  
  &.primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  }
  
  &.success {
    background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
  }
  
  &.warning {
    background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  }
  
  &.info {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  }
}

.quick-content {
  flex: 1;
}

.quick-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.quick-desc {
  font-size: 13px;
  color: #909399;
}

.quick-arrow {
  color: #909399;
  font-size: 18px;
  transition: transform 0.3s;
}

// 响应式设计
@media (max-width: 768px) {
  .statistics-section {
    grid-template-columns: 1fr;
  }
  
  .quick-access-grid {
    grid-template-columns: 1fr;
  }
}
</style> 